using System.Diagnostics;
using SqlSugar;
using System;
using SX.Core;
using System.Collections.Generic;

namespace SX.ProductService.Repository.Core
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class DbContext
    {
        /// <summary>
        /// 用来处理事务多表查询和复杂的操作
        /// </summary>
        public SqlSugarClient Db;  

        /// <summary>
        /// 单例模式
        /// </summary>
        public static SqlSugarScope DbScope = new SqlSugarScope(new ConnectionConfig()
        {
            DbType = SqlSugar.DbType.SqlServer,
            ConnectionString = ConfigHelper.Get("SqlCon", "ConnectionString"),
            IsAutoCloseConnection = true
        });

        public static SqlSugarClient Current
        {
            get
            {
                return new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = ConfigHelper.Get("SqlCon", "ConnectionString"),
                    DbType =DbType.SqlServer,
                    IsAutoCloseConnection =true,
                    InitKeyType =InitKeyType.SystemTable ,   
                    MoreSettings = new ConnMoreSettings()
                    {
                        IsAutoRemoveDataCache = true
                    }
                });
            }
        }

        public DbContext()
        {          
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConfigHelper.Get("SqlCon", "ConnectionString"),
                DbType =DbType.SqlServer,
                IsAutoCloseConnection = true,
                ConfigId=0,
                InitKeyType = InitKeyType.SystemTable,

                MoreSettings = new ConnMoreSettings()
                {
                    IsAutoRemoveDataCache = true
                }
               //从库配置：多个从库 HitRate 越大越有几率走此从库
               //,SlaveConnectionConfigs = new List<SlaveConnectionConfig>() {
               //      new SlaveConnectionConfig() { HitRate=10, ConnectionString="2" } ,
               //      new SlaveConnectionConfig() { HitRate=10, ConnectionString="" }
               // }
            });
            //多库配置
            if(ConfigHelper.Get("SqlCon", "ConnectionString2").NotNull())
            {
                Db.AddConnection(new ConnectionConfig()
                {
                    ConnectionString = ConfigHelper.Get("SqlCon", "ConnectionString2"),
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true,
                    ConfigId = 1,
                    InitKeyType = InitKeyType.SystemTable,
                    MoreSettings = new ConnMoreSettings()
                    {
                        IsAutoRemoveDataCache = true
                    }
                      
                });
            }
        }

        public DbSet<T> DbTable<T>() where T : class, new()
        {
            return new DbSet<T>(Db);
        }
    }

    /// <summary>
    /// 扩展ORM
    /// </summary>
    public class DbSet<T> : SimpleClient<T> where T : class, new()
    {
        public DbSet(SqlSugarClient context) : base(context)
        {

        }
    }

}
